package com.servlet;

import java.io.IOException;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.bean.Constants;
import com.utils.CommonUtil;
import com.utils.GetWxOrderno;
import com.utils.RequestHandler;
import com.utils.Sha1Util;
import com.utils.TenpayUtil;
import com.utils.WeixinUtil;


public class PayServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public PayServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

			doPost(request,response);
	}

	
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * 获取openid
		 */
		String code = request.getParameter("code");
		
		//System.out.println("我是pay :"+code);
		
		String appid = Constants.appid;
		String secret = Constants.appsecret;
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
		
		//System.out.println("pay code === "+ url.toString());
		
		String requestMethod = "GET";
		String outputStr = "";
		String httpRequest = WeixinUtil.httpRequest(url, requestMethod, outputStr);
		JSONObject obj = JSONObject.fromObject(httpRequest);
		String openid = obj.get("openid").toString();
		//System.out.println("openid是"+openid);
		
		
		
		//-------------统一下单---------------
		/**
		 *  预支付
		 */
		RequestHandler reqHandler = new RequestHandler(request, response);
		reqHandler.init(com.bean.Constants.appid, com.bean.Constants.appsecret, com.bean.Constants.key);
		//获取openId后调用统一支付接口https://api.mch.weixin.qq.com/pay/unifiedorder
				String requestXml="";
				try {
					requestXml = CommonUtil.getSign(request, response,reqHandler, request.getRemoteAddr(), openid);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//.out.println(requestXml);
				
				
				
				String createURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
				String prepay_id="";
				try {
					prepay_id = new GetWxOrderno().getPayNo(createURL, requestXml);
					if(prepay_id.equals("")){
						System.out.println("-----------除了错的分割线也是美丽的------------");
						response.sendRedirect("error.jsp");
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				System.out.println("--------------------- 超级华丽分割线 --test  82 ---------------------");
				System.out.println("   订单id是"+prepay_id);
				SortedMap<String, String> finalpackage = new TreeMap<String, String>();
				String appid2 = appid;
				String timestamp = Sha1Util.getTimeStamp();
				String nonceStr2 = CommonUtil.getNonce_str();
				String prepay_id2 = "prepay_id="+prepay_id;
				String packages = prepay_id2;
				finalpackage.put("appId", appid2);  
				finalpackage.put("timeStamp", timestamp);  
				finalpackage.put("nonceStr", nonceStr2);  
				finalpackage.put("package", packages);  
				finalpackage.put("signType", "MD5");
				String finalsign = reqHandler.createSign(finalpackage);
				System.out.println("pay.jsp?appid="+appid2+"&timeStamp="+timestamp+"&nonceStr="+nonceStr2+"&package="+packages+"&sign="+finalsign);
				response.sendRedirect("pay.jsp?appid="+appid2+"&timeStamp="+timestamp+"&nonceStr="+nonceStr2+"&package="+packages+"&sign="+finalsign);
	}


}
